package bb.lanxing.mvp.model;

import static bb.lanxing.activity.routebook.RouteBookInfoActivity.REQUEST_TYPE_ROUTE_BOOK_INFO;

import bb.lanxing.lib.devices.sprint.entity.SprintNav;
import bb.lanxing.lib.devices.sprint.model.impl.AbstractSprintDeviceNavigationModel;
import bb.lanxing.model.database.RouteBook;
import bb.lanxing.mvp.model.i.IHistoryModel;
//import bb.lanxing.network.BiciHttpClient;
import java.io.IOException;

import bb.lanxing.network.MyHttpClient;
import okhttp3.Response;
import org.json.JSONException;
import org.json.JSONObject;
import rx.Observable;

public class SprintDeviceNavigationLushuModel extends AbstractSprintDeviceNavigationModel {
    IHistoryModel historyModel;

    public SprintDeviceNavigationLushuModel(String str) {
        setAddress(str);
    }

    public SprintDeviceNavigationLushuModel() {
    }

    @Override
    protected Observable<SprintNav> loadSprintNav(long j) {
        RouteBook byServerId = RouteBook.getByServerId(j);
        if (byServerId != null) {
            return Observable.just(byServerId);
        }
        if (this.historyModel == null) {
            this.historyModel = new HistoryModelImpl();
        }
        try {
            Response execute = MyHttpClient.requestLushuInfo(j, REQUEST_TYPE_ROUTE_BOOK_INFO).execute();
            if (execute.isSuccessful()) {
                byServerId = RouteBook.parseLushuByServer(new JSONObject(execute.body().string()), true);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (JSONException e2) {
            e2.printStackTrace();
        }
        return Observable.just(byServerId);
    }
}
